go to previous page   go to home page   go to next page

Answer:

fib( 1 ) = 1       (base case)

fib( 2 ) = 1       (base case)

fib( N ) = fib( N-1 ) + fib( N-2 )

Static View

Above is the complete math-like definition for the Fibonacci series. There are two base cases. This is fine. Recursion breaks problems into smaller pieces. After enough breaking, all that remains are the base cases. There can be any number of base cases.

We have a math-like definition. Creating a Java method to implement it should be a mechanical translation from math into Java.


public int fib( int n )
{
  if (  ) 
  
    return ;
    
  else if  (  ) 
  
    return  ;
    
  else
  
    return  +  ;
}

QUESTION 18:

Sharpen your translation skills by filling in those blanks.